package com.yh.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yh.bean.CarApply;
import com.yh.bean.CarManage;
import com.yh.service.impl.CarService;

@Controller
public class CarOperate {
	
	@Autowired
	private CarService csi;
	
		//新增车辆
		@RequestMapping("/addcar")
		@ResponseBody
		public int addcar(CarManage cm){
			System.out.println("新增车辆"+cm.toString());
			int i=csi.addcar(cm);
			return i;
			}
	
		//查询车辆
		@RequestMapping("/querycar")
		@ResponseBody
		public Map querycar(int curr,int pagesize){
			List<CarManage> allcmlist=csi.querycar();
			//进行分页
			Page page=PageHelper.startPage(curr, pagesize);
			
			List<CarManage> cmlist=csi.querycar();
			long totle=page.getTotal();
			Map map=new HashMap();
			map.put("allcmlist", allcmlist);
			map.put("cmlist", cmlist);
			map.put("totle", totle);
			return map;
		}
		
		//修改车辆状态
		@RequestMapping("/updatecar")
		@ResponseBody
		public int updatecar(CarManage cm){
			System.out.println("修改车辆"+cm.toString());
			int i=csi.updatecar(cm);
			return i;
		}
		
		//查看车辆状态
		@RequestMapping("/checkcar")
		@ResponseBody
		public Map checkcar(CarApply ca){
			System.out.println("修改车辆"+ca.toString());
			Map carstatus=csi.checkcar(ca);
			System.out.println("车辆状态"+carstatus);
			return carstatus;
		}
		
		//删除车辆
		@RequestMapping("/delcar")
		@ResponseBody
		public int delcar(String cm_ids){
			System.out.println("删除车辆"+cm_ids);
			String[] cmids=cm_ids.split(",");
			int a=0;
			for(int i=0;i<cmids.length;i++){
				int cm_id=Integer.parseInt(cmids[i].toString());
				a=csi.delcar(cm_id);
				System.out.println(cm_id);
				a*=1;
			}
			return a;
			}
		
		//申请车辆
		@RequestMapping("/carapply")
		@ResponseBody
		public int carapply(CarApply ca){
			System.out.println("申请车辆"+ca.toString());
			Date date=new Date();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy年M月d日H时m分");
			ca.setCa_applytime(sdf.format(date));
			ca.setCa_applystatus("待审核");
			int i=csi.carapply(ca);
			return i;
			}
		//删除车辆
			@RequestMapping("/delcarapply")
			@ResponseBody
			public int delcarapply(int ca_id){
				System.out.println("删除申请"+ca_id);
				int a=csi.delcarapply(ca_id);
				return a;
				}
		//查询车辆
		@RequestMapping("/querycarapply")
		@ResponseBody
		public Map querycarapply(int curr,int pagesize,CarApply ca,HttpSession session){
			System.out.println("查询车辆申请状态"+ca.toString());
			//将当前用户添加到查询条件中
			String username=(String) session.getAttribute("username");
			String usernamerole=(String) session.getAttribute("usernamerole");
			ca.setCa_applyname(username);
			ca.setCa_applynamerole(usernamerole);
			//进行分页
			Page page=PageHelper.startPage(curr, pagesize);
			List<CarApply> querycarapplylist=csi.querycarapply(ca);
			
			long totle=page.getTotal();
			Map map=new HashMap();
			map.put("querycarapplylist", querycarapplylist);
			map.put("totle", totle);
			return map;
		}
		//审批时查询车辆
		@RequestMapping("/querycheckcarapply")
		@ResponseBody
		public Map querycheckcarapply(int curr,int pagesize,CarApply ca,HttpSession session){
			System.out.println("查询车辆申请状态"+ca.toString());
			//进行分页
			Page page=PageHelper.startPage(curr, pagesize);
			List<CarApply> querycarchecklist=csi.querycarapply(ca);
			System.out.println(querycarchecklist);
			long totle=page.getTotal();
			Map map=new HashMap();
			map.put("querycarchecklist", querycarchecklist);
			map.put("totle", totle);
			return map;
		}
		//审批时查询车辆
			@RequestMapping("/querylookcarapply")
			@ResponseBody
			public CarApply querylookcarapply(CarApply ca){
				System.out.println("查询车辆申请xiangqing"+ca.toString());
				CarApply querycarlook=csi.querycarapply(ca).get(0);
				System.out.println(querycarlook);
				return querycarlook;
			}
			
		//审批车辆
		@RequestMapping("/updatecarapply")
		@ResponseBody
		public int updatecarapply(CarApply ca){
			System.out.println("审批车辆"+ca.toString());
			int i=csi.updatecarapply(ca);
			return i;
			}
		
}
